package com.campus.common.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.campus.common.result.Result;
import com.campus.common.teacher.job.entity.Teacher;
import com.campus.common.teacher.job.entity.dto.AddTeacherDTO;
import com.campus.common.teacher.job.vo.TeacherInfoVO;
import com.campus.common.teacher.mapper.TeacherMapper;
import com.campus.common.teacher.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.user.entity.dto.InsertUserDTO;
import com.campus.common.user.entity.pojo.User;
import com.campus.common.user.mapper.AuthMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 教师信息表 服务实现类
 * </p>
 *
 * @author mwb
 * @since 2024-11-17
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private AuthMapper authMapper;

    @Override
    public Result addTeacher(AddTeacherDTO addTeacherDTO) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(addTeacherDTO, teacher);
        int i = baseMapper.insert(teacher);
        if (i == 1) {
            return Result.success();
        }
        return Result.fail("添加教师信息失败");
    }

    @Override
    public Result editTeacher(Teacher teacher) {
        int i = baseMapper.updateById(teacher);
        if (i == 1) {
            return Result.success();
        }
        return Result.fail("修改教师信息失败");
    }

    @Override
    public List<TeacherInfoVO> teacherList() {
        List<TeacherInfoVO> list = new ArrayList<>();
        List<Teacher> teacherList = teacherMapper.selectList(null);
        for (Teacher teacher : teacherList) {
            User user = authMapper.selectById(teacher.getUserId());
            TeacherInfoVO vo = new TeacherInfoVO();
            vo.setId(teacher.getId());
            vo.setTeacherName(user.getName());
            list.add(vo);
        }
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result insertTeacher(InsertUserDTO insertUserDTO) {
        User user = new User();
        user.setIdentity(1);
        String workId = UUID.randomUUID().toString();
        user.setWorkId(workId);
        user.setUsername(insertUserDTO.getUsername());
        user.setPassword(insertUserDTO.getPassword());
        user.setName(insertUserDTO.getName());
        user.setSex(insertUserDTO.getSex());
        user.setRegisterDate(LocalDateTime.now());
//        user.setAge(insertUserDTO.getAge());
        int i = authMapper.insert(user);
        if (i > 0) {
            User user1 = authMapper.selectOne(new LambdaQueryWrapper<User>()
                    .eq(User::getWorkId, workId));

            Teacher teacher = new Teacher();
            teacher.setOffice(insertUserDTO.getOffice());
            teacher.setAcademy(insertUserDTO.getAcademy());
            teacher.setUserId(user1.getId());
            int insert = teacherMapper.insert(teacher);
            if (insert > 0) {
                return Result.success(insertUserDTO);
            } else {
                return Result.fail("插入教师信息失败");
            }
        } else {
            return Result.fail("插入用户失败");
        }

    }
}
